<?php
class ErrorEntity
{
    public    $errCode;
    public    $param;
    protected $properties;

    /** __construct ========================
     * @author TuanNguyen
     * @desc   set param of class to default
     *         use wizard of PHP
     * =====================================*/
    function __construct()
    {
        $this->errCode    = null;
        $this->param      = null;
        $this->properties = array();
    }

    /** __get =================================
     * @author TuanNguyen
     * @desc   get value of properties of class
     *         use wizard of PHP method
     * ========================================*/
    function __get($propety)
    {
        return $this->properties[$propety];        
    }

    /** __set =================================
     * @author TuanNguyen
     * @desc   set value of properties of class
     *         use wizard of PHP method
     * ========================================*/
    function __set($property, $value)
    {
        $this->properties[$property] = $value;
    }
    
    /** setErrorMessage ========================================
     * @author   TuanNguyen
     * @desc     loop all error entity in array
     *           set error messages in array of error message
     * @param    array $arr_errorEnts: array of error entity
     *           array $arr_errorMessage: array of error message
     * @return   string
     * =========================================================*/
    public static function setErrorMessageByArray(array $arr_errorEnts, array $arr_errorMessage)
    {
        if( !is_array($arr_errorEnts) || count($arr_errorEnts) < 1 ) {
            return "There are an errors but system don't set it to array of error";
        }

        //Init parameter
        $s_message = "";

        foreach ( $arr_errorEnts as $index=>$o_error )
        {
            if ( $o_error instanceof ErrorEntity )
            {
                if ( is_null($o_error->param) )
                {
                    $s_message .= $arr_errorMessage[$o_error->errCode]."\r";
                }
                elseif ( is_array($o_error->param) && count($o_error->param)>0 )
                {
                    $s_temp = $arr_errorMessage[$o_error->errCode];

                    foreach ( $o_error->param as $index=>$value ) {
                        $s_temp = str_replace($index, $value, $s_temp);
                    }

                    $s_message .= $s_temp."\r";
                }//End elseIf
            }//End if check ErrorEntity
            else
            {
                return "There are an unknow error entity in function get error message";
            }
        }//End foreach

        return $s_message;
    }//End setErrorMessageByArray

    /** setErrorMessage =============================
     * @author   TuanNguyen
     * @desc     loop all error entity in array
     *           set error messages in array of error message
     * @param    ErrorEntity $o_error: error entity
     *           array $arr_errorMessage: array of error message
     * @return   string
     * ============================================== */
    public static function setErrorMessageByEntity(ErrorEntity $o_error, array $arr_errorMessage)
    {
        //Init parameter
        $s_message = "";

        if ( is_null($o_error->param) )
        {
            $s_message .= $arr_errorMessage[$o_error->errCode]."\r";
        }
        elseif ( is_array($o_error->param) && count($o_error->param)>0 )
        {
            $s_temp = $arr_errorMessage[$o_error->errCode];

            foreach ( $o_error->param as $index=>$value )
            {
                $s_temp = str_replace($index, $value, $s_temp);
            }

            $s_message .= $s_temp."\r";
        }//End elseIf

        return $s_message;
    }//End setErrorMessageByEntity

    /** isErrorEntity =============================
     * @author   TuanNguyen
     * @desc     check if value is member of error entity
     * @param    object $o_error
     * @return   boolean
     * ============================================== */
    public static function isErrorEntity($o_error)
    {
        //Is Error Entity
        if( $o_error instanceof ErrorEntity ) {
            return true;
        }

        //Not Error Entity
        return false;
    }//End isErrorEntity

}